Ext.require([
    'Ext.window.MessageBox',
    'Ext.tip.*'
]);
Ext.define('AM.view.DepartmentView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.departmentView',
		store : 'DepartmentStore',
//		frame:true,
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"     //"SINGLE"/"SIMPLE"/"MULTI"
	     
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'DepartmentStore',
        displayInfo: true
        }],
		columns: [
			        { text: '序号', dataIndex: 'id',width:50,align:'center'},
			        { text: '名称', dataIndex: 'name', width:200,align:'center'},
			        { text: '科室类型', dataIndex: 'xzorzy', width:200,align:'center',renderer: function(value){
					        	 if (value === 0) {
										 return '行政科室';
								 }if (value === 1) {
										 return '住院科室';
								 }
								 
								 }}
			        
			     ],
			   tbar: [
					  { xtype: 'button', id:'tab-8_add',text: '增加',handler: addDepartment, icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', id:'tab-8_edit',text: '修改',handler: editDepartment,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button',id:'tab-8_delete', text: '删除',handler: deleteDepartment,icon: 'Ext/restful/images/delete.png'
					  }
					]
	
})
//    


   function addDepartment() {
   		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({
		 			bodyPadding: 30,
			        labelWidth : 65,
					   items: [{   
					   		      columnWidth :.46,
					   		      allowBlank:false,
					   			  xtype : "textfield",
					   		      fieldLabel: '科室名称',
					   			  labelAlign :"right",
							      name: 'name'
							      
							},{
								
						        			columnWidth :.3,
								        	layout : "form",
								        	border : false,
								        	items:[{
								        	 	xtype: 'radiogroup',
							        			labelAlign :"right",
										        fieldLabel: '科室类型',
										        columns: 2,
									
										        vertical: true,
										        items: [
										            { boxLabel: '行政', name: 'xzorzy', inputValue: '0' , checked: true},
										            { boxLabel: '住院', name: 'xzorzy', inputValue: '1'}
										        ]
								        	 }]
						        			
						        		
							
							}]		        	
				        	       

		 })
			var win = new Ext.Window({
				title : "添加科室",
				width : 350,
				height : 200,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'department_addDepartment',
											 success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												grid.getStore().reload();//jobView 为id号
//												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		win.show();
	}



   function editDepartment() {
   		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({

			        bodyPadding: 20,
			        labelWidth : 65,
			        items: [{   
					   			  xtype : "textfield",
					   			  allowBlank:false,
					   		      fieldLabel: '科室名称',
					   			  labelAlign :"right",
							      name: 'name',
							      width:300
							},{
								
						        			columnWidth :.3,
								        	layout : "form",
								        	border : false,
								        	items:[{
								        	 	xtype: 'radiogroup',
							        			labelAlign :"right",
										        fieldLabel: '科室类型',
										        columns: 2,
									
										        vertical: true,
										        items: [
										            { boxLabel: '行政', name: 'xzorzy', inputValue: '0' },
										            { boxLabel: '住院', name: 'xzorzy', inputValue: '1'}
										        ]
								        	 }]
						        			
						        		
							
							
							
							
							},{
								  xtype : "textfield",
					   		      fieldLabel: '科室id',
					   			  labelAlign :"right",
							      name: 'id',
							      hidden : true 
							}]		        	
		
		 })
			var win = new Ext.Window({
				title : "修改科室",
				width : 350,
				height : 200,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'department_editDepartment',
											 success : function() {
											 	
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
							    grid.getSelectionModel().deselectAll();		
							}
						}]
			});
//		var grid=this.ownerCt.ownerCt;
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deleteDepartment(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'department_delete',
										success : function() {
											Ext.MessageBox.alert("提示",
													"删除" + record.get("name")+ "成功！");
													 grid.getStore().remove(selections );
												     grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "科室被引用，删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}else{
									 grid.getSelectionModel().deselectAll();		
								}
					  	})
                   	} 
 			   
		   }				
	}
	 	

	